package cn.smbms.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import cn.smbms.bean.model.Staff;
import cn.smbms.bean.vo.StaffVo;
import cn.smbms.dao.IStaffDao;

public class IStaffDaoImpl extends HibernateDaoSupport implements IStaffDao{

	//多条件查询
	@Override
	public List<Staff> getStaffByStaffVo(final StaffVo staffVo) {
		return getHibernateTemplate().execute(new HibernateCallback<List<Staff>>() {
			@Override
			public List<Staff> doInHibernate(Session session)throws HibernateException, SQLException {
				Query query = session.createSQLQuery("select * from Staff where 1=1 and chnName like :name and createTime >= :begin and createTime <= :end");
				query.setParameter("name", "%" + staffVo.getChnName() + "%");
				query.setParameter("begin",staffVo.getBegin());
				query.setParameter("end",staffVo.getEndDate());
				query.setFirstResult(0);
				query.setMaxResults(2);
				return query.list();
			}
		});
	}

	//添加用户
	@Override
	public int add(Staff staff) {
		return (int) getHibernateTemplate().save(staff);
	}

	//根据用户帐号查询用户信息
	@Override
	public Staff findStaffByStaffNum(final String staffNum) {
		return getHibernateTemplate().execute(new HibernateCallback<Staff>() {
			@Override
			public Staff doInHibernate(Session session)throws HibernateException, SQLException {
				Query query = session.createQuery(" from Staff where staffNum=:staffNum");
				query.setParameter("staffNum", staffNum);
				return (Staff) query.uniqueResult();
			}
		});
	}

	//更新用户信息
	@Override
	public int update(Staff staff) {
		getHibernateTemplate().update(staff);
		return 1;
	}

	//删除用户信息
	@Override
	public int delete(int staffId) {
		//先获取Staff对象，getHibernateTemplate().get(Staff.class, staffId)
		getHibernateTemplate().delete(getHibernateTemplate().get(Staff.class, staffId));
		return 1;
	}

	//用户登录验证
	@Override
	public Staff checkLogin(final Staff staff) {
		return getHibernateTemplate().execute(new HibernateCallback<Staff>() {
			@Override
			public Staff doInHibernate(Session session)throws HibernateException, SQLException {
				Query query = session.createQuery(" from Staff where staffNum=:staffNum and pwd=:pwd");
				query.setParameter("staffNum", staff.getStaffNum());
				query.setParameter("pwd", staff.getPwd());
				return (Staff) query.uniqueResult();
			}
		});
	}

	//获取总页
	@Override
	public StaffVo getTotalPages(final StaffVo staffVo) {
		return getHibernateTemplate().execute(new HibernateCallback<StaffVo>() {
			@Override
			public StaffVo doInHibernate(Session session)throws HibernateException, SQLException {
				Query query = session.createSQLQuery("select count(*) as totalPages from Staff where 1=1 and chnName like :name and createTime >= :begin and createTime <= :end");
				query.setParameter("name", "%" + staffVo.getChnName() + "%");
				query.setParameter("begin",staffVo.getBegin());
				query.setParameter("end",staffVo.getEndDate());
				staffVo.setTotalPages(Integer.parseInt(query.uniqueResult().toString()));
				return staffVo;
			}
		});
	}
}
